﻿using CampusTrade.Pojo;
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CampusTrade.Dao
{
    public class TransactionDao : ITransient
    {
        private readonly ISqlSugarRepository repository;
        private readonly SqlSugarClient db;
        public TransactionDao(ISqlSugarRepository sqlSugarRepository)
        {
            repository = sqlSugarRepository;
            db = repository.Context;
        }
        public Transaction select(long id)
        {
            try
            {
                return db.Queryable<Transaction>().First(it => it.Id == id);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public long add(Transaction transaction)
        {
            try
            {
                return db.Insertable(transaction).ExecuteReturnBigIdentity();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }
        public int delete(long id)
        {
            try
            {
                return db.Deleteable<Transaction>().In(id).ExecuteCommand();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public List<Transaction> mySellList(long uid)
        {
            try
            {
                List<Transaction> sellList = db.Queryable<Transaction, Good>((t, g) => new JoinQueryInfos(JoinType.Left, t.GoodId == g.Id))
                    .Where((t, g) => t.TradeUserId == uid && g.Type == 0)
                    .Mapper(t =>
                    {
                        t.Good = db.Queryable<Good>().Where(g => g.Id == t.GoodId).First();
                    })
                    .Mapper(t =>
                    {
                        t.TradeUser = db.Queryable<User>().Where(u => u.Id == uid).First();
                    })
                    .ToList();
                return sellList;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public List<Transaction> myBuyList(long uid)
        {
            try
            {
                List<Transaction> buyList = db.Queryable<Transaction, Good>((t, g) => new JoinQueryInfos(JoinType.Left, t.GoodId == g.Id))
                    .Where((t, g) => t.TradeUserId == uid && g.Type == 1)
                    .Mapper(t =>
                    {
                        t.Good = db.Queryable<Good>().Where(g => g.Id == t.GoodId).First();
                    })
                    .Mapper(t =>
                    {
                        t.TradeUser = db.Queryable<User>().Where(u => u.Id == uid).First();
                    })
                    .ToList();
                return buyList;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }
}
